/**
 * Copyright 2013-2017 JueYue (qrb.jueyue@gmail.com)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.chcit.share.generator.factory.impl;


import com.alibaba.fastjson.JSONObject;
import com.chcit.share.generator.constants.GeneratorConstant;
import com.chcit.share.generator.factory.IParse;
import com.chcit.share.generator.model.TemplateModel;
import com.chcit.share.generator.vo.GenerationEntity;
import com.chcit.share.generator.vo.TableModel;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

/**
 * 解析模板,生产代码
 * @author liuyang
 */
public class BeetlParseImpl implements IParse {

    private static final Logger LOGGER = LoggerFactory.getLogger(BeetlParseImpl.class);

    private static final StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();

    private static Configuration cfg;
    private static GroupTemplate gt;

    static {
        try {
            cfg = Configuration.defaultConfiguration();
            gt = new GroupTemplate(resourceLoader, cfg);
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }

    @Override
    public String parse(GenerationEntity generationEntity, TableModel tableEntity, TemplateModel templateModel) {

        Template t = gt.getTemplate(templateModel.getTemplateContent());
        String gen = null;
        String table = null;
        String params = templateModel.getParams();
        JSONObject json = null;
        if (params != null) {
            json = JSONObject.parseObject(params);
        }
        if (json != null) {
            gen = json.getString(GeneratorConstant.GEN_PARAMS);
            table = json.getString(GeneratorConstant.TABLE_DETAIL);

        }
        if (gen == null) {
            gen = IParse.GEN_PARAMS;
        } else {
            json.remove(GeneratorConstant.GEN_PARAMS);
        }
        if (table == null) {
            table = IParse.TABLE_DETAIL;
        } else {
            json.remove(GeneratorConstant.TABLE_DETAIL);
        }
        t.binding(gen, generationEntity);
        t.binding(table, tableEntity);
        if (json != null) {
            for (Map.Entry<String, Object> entry : json.entrySet()) {
                t.binding(entry.getKey(), entry.getValue());
            }
        }


        return t.render();
    }

}
